// pages/mine/payment/eletric/eletricDetail/eletricDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:1,//1:未缴，2：已缴 3:补缴
    typeImg:"",
    type:1,
    id:null,//详情ID
    detailInfo:{},
    typeTitle:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      type: options.type,
      status: options.status,
      id: options.id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetailInfo()
    let pageTotal = getCurrentPages()
  },
  /**
   * 生命周期函数--监听页面渲染
   */
  onReady: function () {
    //设置title和图片
    this.setType(this.data.type)
  },
  // 根据不同type设置不同的标题和图片
  setType(type) {
    switch (type) {
      case "1":
        this.setData({ type: 1, typeImg: "/images/payment/eletric.png", typeTitle:"电费" })
        wx.setNavigationBarTitle({
          title: '电费',
        })
        break;
      case "2":
        this.setData({ type: 2, typeImg: "/images/payment/water.png", typeTitle: "水费"  })
        wx.setNavigationBarTitle({
          title: '水费',
        })
        break;
      case "3":
        this.setData({ type: 3, typeImg: "/images/payment/gas.png", typeTitle: "燃气费"  })
        wx.setNavigationBarTitle({
          title: '燃气费',
        })
        break;
      case "4":
        this.setData({ type: 4, typeImg: "/images/payment/net.png", typeTitle: "网费"  })
        wx.setNavigationBarTitle({
          title: '网费',
        })
        break;
      case "5":
        this.setData({ type: 5, typeImg: "/images/payment/manage.png", typeTitle: "物业费"  })
        wx.setNavigationBarTitle({
          title: '物业费',
        })
        break;
    }
  },
  /**
   * 页面缴费信息
   */
  getDetailInfo(){
    app.globalData.request("post","/applet/wx_pay/billDetail",{
      token: wx.getStorageSync("token"),
      type: this.data.type,
      id: this.data.id
    })
    .then(res => {
      this.setData({ detailInfo:res.data })
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: "none"
      })
    })
  },
  /**
   * 缴费
   */
  payFees(e){
    console.log(e)
    app.globalData.request('post', '/applet/wx_pay/pay',{
      id:wx.getStorageSync("id"),
      bill_id: e.currentTarget.dataset.bid,
      type: e.currentTarget.dataset.type,
      total_fee: e.currentTarget.dataset.fee
    }).then(res => {
      wx.requestPayment({
        timeStamp: res.msg.timeStamp,
        nonceStr: res.msg.nonceStr,
        package: res.msg.package,
        signType: res.msg.signType,
        paySign: res.msg.paySign,
        success(res) {
          wx.showModal({
            title: '支付成功!',
            showCancel: false,
            success(res){
              if(res.confirm){
                let pageTotal = getCurrentPages()
                let thispage = pageTotal[pageTotal.length - 1]
                //根据pagetotal.length判断是否是从列表进来的
                if(pageTotal.length == 4){
                  let eleIndex = pageTotal.findIndex((item, index) => {
                    return item.route == "pages/mine/payment/eletric/eletric"
                  })
                  let eletricpage = pageTotal[eleIndex]
                  eletricpage.setData({
                    parameter:"paySuccess"
                  })
                }else if(pageTotal.length == 3){
                  let paymentIndex = pageTotal.findIndex((item, index) => {
                    return item.route == "pages/mine/payment/payment"
                  })
                  let paymentpage = pageTotal[paymentIndex]
                  paymentpage.setData({
                    parameter:"paySuccess"
                  })
                }
                wx.navigateBack({
                  delta:1
                })
              }
            }
          })  
        },
        fail(err) {
          wx.showModal({
            title: '支付失败!',
            content: err
          })
        }
      })
    }).catch(err => {
      console.log(err)
    })
  },
})